//
//  RCWelcomeVC.m
//  YBar
//
//  Created by Rich on 16/4/19.
//  Copyright © 2016年 Rich. All rights reserved.
//

#import "RCWelcomeVC.h"

@interface RCWelcomeVC()<UIWebViewDelegate>

@property(nonatomic,strong) UIWebView *webView;

@end

@implementation RCWelcomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadWebView];
    [self loadNativeWelcomeView];
}

#pragma mark - 原生欢迎界面

- (void)loadNativeWelcomeView
{
    NSArray *adImgArr = @[@"wel1",@"wel2",@"wel3"];
    UIScrollView *welcomeScroll = [[UIScrollView alloc]init];
    welcomeScroll.frame = self.view.frame;
    welcomeScroll.contentSize = CGSizeMake(SCREEN_WIDTH*adImgArr.count, SCREEN_HEIGHT);
    welcomeScroll.pagingEnabled = YES;
    welcomeScroll.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:welcomeScroll];
    CGFloat btnWidth = 100;
    CGFloat btnHeight = 30;
    CGFloat btnOriginX = (SCREEN_WIDTH - 100)/2;
    CGFloat btnOriginY = SCREEN_HEIGHT - 100;
    UIButton *enterBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*(adImgArr.count - 1) + btnOriginX, btnOriginY, btnWidth, btnHeight)];
    [enterBtn addTarget:self action:@selector(enterHome) forControlEvents:UIControlEventTouchUpInside];
    [welcomeScroll addSubview:enterBtn];
    
    for (int i = 0;i < adImgArr.count;i ++)
    {
        NSString *imageName = adImgArr[i];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(SCREEN_WIDTH*i, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [welcomeScroll addSubview:imageView];
    }
    
}

- (void)enterHome
{
    [[NSNotificationCenter defaultCenter] postNotificationName:ResetRootVCNotification object:nil];
    
}


#pragma mark - 利用webview做欢迎界面

- (void)loadWebView
{
    [self createWebView];
    NSString *strResourcesBundle = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"];
    NSString *strPath = [[NSBundle bundleWithPath:strResourcesBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"Welcome"];
    NSURL *url = [NSURL fileURLWithPath:strPath];
    [_webView loadRequest:[NSURLRequest requestWithURL:url]];
    
}

- (void)createWebView
{
    if (self.webView == nil) {
        _webView = [[UIWebView alloc] initWithFrame:self.view.frame];
        self.webView.delegate = self;
        [self.view addSubview:self.webView];
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    NSString *requestString = [[request URL] absoluteString];
    NSArray *components = [requestString componentsSeparatedByString:@":"];
    if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"start"]) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ResetRootVCNotification object:nil];
        return NO;
    }
    return YES;
}


@end
